package com.ciban.smarthome.module.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.db.dao.LampDao;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.Equip_Lamp;
import com.ciban.smarthome.module.entity.Equipment;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/19.
 */
public class SceneEListAdapter extends BaseExpandableListAdapter {
    public interface EquipChangedListener {
        void equipStatusChanged(String sceneID, String equipID, int type, int status);

        <T> T getEquipConfigDetails(String sceneID, String equipID, int type);
    }


    private ArrayList<Area> list;
    private ArrayList<ArrayList<Equipment>> childs;
    private Context context;
    private String scendID;

    private EquipChangedListener listener;

    public SceneEListAdapter(Context context, String scendID, ArrayList<Area> areaList, ArrayList<ArrayList<Equipment>> childList) {
        this.context = context;
        this.list = areaList;
        this.childs = childList;
        this.scendID = scendID;
    }

    public void setListener(EquipChangedListener listener) {
        this.listener = listener;
    }


    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }


    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder gHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.listitem_scene_group, null);
            gHolder = new GroupHolder();
            gHolder.tv_title = (TextView) convertView.findViewById(R.id.scene_diy_group_title);
            convertView.setTag(gHolder);
        } else {
            gHolder = (GroupHolder) convertView.getTag();
        }

        Area area = list.get(groupPosition);
        gHolder.tv_title.setText(area.getName());
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.listitem_scene_childs, null);
            holder = new ChildHolder();
            holder.ll_default = (LinearLayout) convertView.findViewById(R.id.scene_diy_child_default);
            holder.common_name = (TextView) convertView.findViewById(R.id.tv_scene_child_equipname);
            holder.common_status = (CheckBox) convertView.findViewById(R.id.cb_scene_child_equipstatus);

            holder.ll_lamp = (LinearLayout) convertView.findViewById(R.id.scene_diy_child_lamp);
            holder.lamp_level = (SeekBar) convertView.findViewById(R.id.scene_diy_child_lamp_seek);

            holder.common_status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Equipment info = childs.get(groupPosition).get(childPosition);
                    if (listener != null)
                        listener.equipStatusChanged(scendID, info.getId(), info.getType(), isChecked ? Constants.STATUS_COMMON_OPEN : Constants.STATUS_COMMON_CLOSE);
                }
            });

            holder.lamp_level.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        Equipment equipment = childs.get(groupPosition).get(childPosition);

        holder.common_name.setText(equipment.getName());

        clearChildView(holder);
        switch (equipment.getType()) {
            case 0:
                holder.ll_lamp.setVisibility(View.VISIBLE);
                Equip_Lamp info = null;
                if (listener != null)
                    info = listener.getEquipConfigDetails(scendID, equipment.getId(), equipment.getType());
                if (info != null) {
                    holder.lamp_level.setProgress(info.getLevel() > 0 ? info.getLevel() : 0);
                }
                break;
            case 1:
                break;
            case 2:
                break;
            default:
                holder.ll_default.setVisibility(View.VISIBLE);
                break;
        }
        return convertView;
    }

    private void clearChildView(ChildHolder holder) {
        holder.ll_lamp.setVisibility(View.GONE);
        holder.ll_default.setVisibility(View.GONE);
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupHolder {
        TextView tv_title;
    }

    static class ChildHolder {
        LinearLayout ll_default;
        TextView common_name;
        CheckBox common_status;
        LinearLayout ll_lamp;
        SeekBar lamp_level;
    }
}
